#include <stdio.h>

int bubble_sort(int d[], int n)
{
    int i, j, count = 0;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (d[j] > d[j + 1])
            {
                int t = d[j];
                d[j] = d[j + 1];
                d[j + 1] = t;
                count++;
            }
        }
    }
    return count;
}

int main()
{
    int N, d[1000];
    while (scanf("%d", &N) == 1 && N != 0)
    {
        int i;
        for (i = 0; i < N; i++)
            scanf("%d", d + i);
        printf("%d\n", bubble_sort(d, N));
    }
    return 0;
}
